<?php

include('../config.inc');
require('dummy-app.inc');

if (empty($_GET)) {
	session_start();
	$tree = make_app("Sign Up");
	$app = $tree->app;
	$app->h2 = "Sign Up";
	$app->form['action'] = 'app/register.php';
	$form = $app->form;
	$form->label[] = 'Choose user ID: ';
	add_child($form, 'input', 
		array('name' => 'uid', 'type' => 'text'));
	$form->label[] = 'Choose password: ';
	add_child($form, 'input', 
		array('name' => 'upass', 'type' => 'password'));
	$form->label[] = 'Retype password: ';
	add_child($form, 'input', 
		array('name' => 'upass2', 'type' => 'password'));
	$form->div['id'] = 'signup_left';
	$form->div['class'] = 'layout_left';
	$form->div->p[] = 
		'If you are our offline customer, sign up here:';
	$form->div->label[] = 'Your offline Customer Number: ';
	add_child($form->div, 'input', 
		array('name' => 'unum', 'type' => 'text'));
	add_child($form->div, 'input', 
		array('type' => 'submit', 'value' => 'Submit'));
	$form->div[]['id'] = 'signup_right';
	$form->div[1]['class'] = 'layout_right';
	$form->div[1]->p[] = 
		'Or your can fill out your personal information here:';
	$form->div[1]->label[] = 'Full Name: ';
	add_child($form->div[1], 'input', 
		array('name' => 'uname', 'type' => 'text'));
	$form->div[1]->label[] = 'Address: ';
	add_child($form->div[1], 'input', 
		array('name' => 'addr', 'type' => 'text'));
	$form->div[1]->label[] = 'Phone Number: ';
	add_child($form->div[1], 'input', 
		array('name' => 'phone', 'type' => 'text'));
	$form->div[1]->label[] = 'Email: ';
	add_child($form->div[1], 'input', 
		array('name' => 'email', 'type' => 'text'));
	add_child($form->div[1], 'input', 
		array('type' => 'submit', 'value' => 'Submit'));
} elseif ($_COOKIE['uname']) {
	$tree = make_app("Sign Up :: Rejected");
	$app = $tree->app;
	$app->addChild('h2', "You are already a registered customer");
	$app->addChild('p', "So help us to save the server resources please.");
} elseif ($_GET['uid'] and $_GET['upass'] and $_COOKIE[session_name()]) {
	!user_info_exists('ID', $_GET['uid']) or 
		exit_404("Duplicate UID");
	$_GET['upass'] == $_GET['upass2'] or 
		exit_404("Unmatched Password");
	if ($_GET['uname']) {
		user_add($_GET['uid'], $_GET['upass'], 
			array($_GET['uname'], $_GET['addr'], 
			$_GET['phone'], $_GET['email'])) or 
			exit_404("Add user failed");
	} elseif ($_GET['unum']) {
		is_null(user_info_exists('CustomerNum', $_GET['unum'])) or 
			exit_404("Unusable Customer Number");
		user_add($_GET['uid'], $_GET['upass'], $_GET['unum']) or 
			exit_404("Add user failed");
	} else exit_404('Please provide your Real Name 
		if you are not our offline customer.');
	$tree = make_app("Sign Up :: Succeeded");
	$app = $tree->app;
	$app->addChild('h2', "Congratulations!");
	$app->addChild('p', sprintf
		("Now you have successfully registered as %s.", $_GET['uid']));
	$app->addChild('p')
		->addChild('a', htmlentities("Looking for a car?"));
	$app->p[1]->a['href'] = "javascript:load_app('search')";
} else exit_404("Bad request");

function user_info_exists($field, $value, $quote = true) {
	$user = mysql_fetch_assoc(mysql_query(sprintf("select $field, ID 
		from Login_Info right join Customer using(CustomerNum) 
		where $field = %s;", db_escape($value, $quote))));
	return $user ? $user['ID'] : '';
}

function user_add($uid, $upass, $info) {
	switch (gettype($info)) {
	case 'string':
		return mysql_query("insert into Login_Info 
			values ('$uid', $info, '$upass');");
	case 'array':
		if (mysql_query(sprintf("insert into Customer values (0, %s);", 
			implode(', ', array_map(db_escape, $info)))))
			return mysql_query(sprintf("insert into Login_Info 
				values ('$uid', %s, '$upass');", mysql_insert_id()));
	}
	return false;
}

echo $tree->asXML();
?>
